package org.example.ui.customer;

import org.example.business.OrderBusiness;
import org.example.business.UserBusiness;
import org.example.manage.MilkTeaManage;
import org.example.manage.OrderManage;
import org.example.model.MilkTea;
import org.example.model.Order;
import org.example.model.OrderItem;
import org.example.ui.employee.ManageOrderPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class SettlementPanel extends JPanel {
    private JLabel promptLabel, totalLabel;
    private JList milkList;
    private JScrollPane scrollPane;
    public SettlementPanel(JDialog dialog, Order order, int type) {
        setLayout(null);
        setBounds(0,0,500,600);
        if (type == 2|| type == 3) {
            JButton confirm = new JButton("支付");
            add(confirm);
            confirm.setFont(new Font("", Font.BOLD, 20));
            confirm.setBounds(400, 500, 80, 40);
            confirm.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    order.condition = "进行中";
                    order.mailbox = UserBusiness.user.mailbox;
                    List<MilkTea> list = new MilkTeaManage().read();
                    for (OrderItem item : order.orderItems) {
                        for (MilkTea tea : list) {
                            if (tea.milkName.equals(item.milkName)&&
                            tea.price == item.price&&tea.imagePath.equals(item.imagePath)) {
                                tea.inventory -= item.number;
                            }
                        }
                    }
                    MilkTeaManage manage = new MilkTeaManage();
                    manage.objectList = list;
                    manage.save(null);
                    new OrderManage("data/order_file/" + UserBusiness.user.orderPath).save(order);
                    new OrderManage("data/order_file/need_do_order.json").save(order);
                    JOptionPane.showMessageDialog(SettlementPanel.this, "支付成功");
                    if (type == 2) {
                        OrderPanel.clean();
                    }
                    dialog.dispose();
                    JOptionPane.showMessageDialog(SettlementPanel.this, "您的订单正在制作中，请耐心等候");
                    OrderBusiness.updateOrders();
                    ManageOrderPanel.setAdminItems();
                }
            });
            promptLabel = new JLabel("您有一个订单，请支付");
        }
        else {
            if (order.condition.equals("已取消")) {
                promptLabel = new JLabel("您的订单已取消，祝你生活愉快");
            }
            else if (order.condition.equals("进行中")) {
                promptLabel = new JLabel("您的订单正在制作中，请耐心等候");
            }
            else {
                promptLabel = new JLabel("您的订单已完成，祝你用餐愉快");
            }
        }
        promptLabel.setFont(new Font("",Font.BOLD,25));
        promptLabel.setBounds(0,0,400,100);
        add(promptLabel);
        totalLabel = new JLabel("共" + order.total + "件"
                + "总计￥" + order.totalPrice);
        totalLabel.setFont(new Font("",Font.BOLD,15));
        totalLabel.setBounds(250,300,200,30);
        add(totalLabel);
        DefaultListModel model = new DefaultListModel();
        for(int i = 0; i < order.orderItems.size(); i++){
            model.addElement(order.orderItems.get(i));
        }
        milkList = new JList(model);
        milkList.setCellRenderer(new MilkCell());
        scrollPane = new JScrollPane(milkList);
        scrollPane.setBounds(0,100,500,200);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        add(scrollPane);
    }
    public class MilkCell extends JLabel implements ListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            OrderItem orderItem = (OrderItem) value;
            Image image = new ImageIcon(orderItem.imagePath).
                    getImage().getScaledInstance(100,100,Image.SCALE_DEFAULT);
            setIcon(new ImageIcon(image));
            String str = "<html>" + "<h2>"  + orderItem.milkName + "</h2>"  +
                   "<font color = red>" + "￥" + orderItem.price + "<br></font>" + orderItem.specification +
                    "<br>" + "x" + orderItem.number + "</html>";
            setText(str);
            setBorder(BorderFactory.createLoweredSoftBevelBorder());
            return this;
        }
    }

}
